package com.zhicheng.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhicheng.backend.common.ArticleFilter;
import com.zhicheng.backend.common.Result;
import com.zhicheng.backend.entity.Article;
import com.zhicheng.backend.entity.User;
import com.zhicheng.backend.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:搜索控制器
 * 作者:YukiSu
 * 日期:2024/12/4 20:23
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    ArticleService articleService;
    ArticleFilter articleFilter;
    @Autowired
    public SearchController(ArticleService articleService, ArticleFilter articleFilter) {
        this.articleService = articleService;
        this.articleFilter = articleFilter;
    }

//    文章搜索接口
    @GetMapping("/article")
    public Result searchArticleLikeTitle(@RequestParam String title, @RequestParam Integer page) {
        IPage<Article> iPage = new Page<>(page, 10);
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.like("title", title);
        IPage<Article> articleIPage = articleService.page(iPage, articleQueryWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("count", articleService.count(articleQueryWrapper));
        result.put("list", articleIPage.getRecords());
        return Result.success(result);
    }

    public Result searchArticleLikes(ArticleFilter articleFilter) {
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if (articleFilter.getAuthor().isEmpty()) {

        }
        return null;
    }

}